import 'package:flutter/material.dart';
import 'package:mall/screens/store/components/store_category.dart';
import 'package:mall/screens/store/components/store_contact.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../theme.dart';
import 'components/store_home.dart';
import 'components/store_product.dart';

/// 店铺
class StoreScreen extends StatefulWidget {
  static const routeName = "/StoreScreen";
  final int args;

  const StoreScreen({
    Key key,
    @required this.args,
  }) : super(key: key);

  @override
  _StoreScreenState createState() => _StoreScreenState();
}

class _StoreScreenState extends State<StoreScreen> {
  PageController _pageController;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(),
      body: _buildBody(),
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }

  AppBar _buildAppBar() {
    return AppBar(
      automaticallyImplyLeading: false,
      title: Text("店铺"),
      actions: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w),
          decoration: BoxDecoration(
            border: Border.all(color: kLineColor),
            borderRadius: BorderRadius.circular(20.w),
          ),
          margin: EdgeInsets.all(13.w),
          child: Row(
            children: [
              // 更多
              Icon(Icons.more_horiz, size: 17.sp),
              VerticalDivider(indent: 4.w, endIndent: 4.w),
              // 关闭
              GestureDetector(
                onTap: () => Navigator.of(context).pop(),
                child: Icon(Icons.close, size: 17.sp),
              ),
            ],
          ),
        ),
      ],
    );
  }

  PageView _buildBody() {
    return PageView.builder(
      controller: _pageController,
      physics: NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        switch (index) {
          case 0:
            return StoreHome(storeId: widget.args);
          case 1:
            return StoreProduct(storeId: widget.args);
          case 2:
            return StoreCategory(storeId: widget.args);
          default:
            return StoreContact();
        }
      },
      itemCount: 4,
    );
  }

  BottomNavigationBar _buildBottomNavigationBar() {
    return BottomNavigationBar(
      currentIndex: _currentIndex,
      onTap: (value) {
        _pageController.jumpToPage(value);
        setState(() {
          _currentIndex = value;
        });
      },
      items: [
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icons/home.png")),
          activeIcon: ImageIcon(AssetImage("assets/icons/home_active.png")),
          label: "店铺首页",
        ),
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icons/product.png")),
          activeIcon: ImageIcon(AssetImage("assets/icons/product_active.png")),
          label: "全部宝贝",
        ),
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icons/category.png")),
          activeIcon: ImageIcon(AssetImage("assets/icons/category_active.png")),
          label: "分类",
        ),
        BottomNavigationBarItem(
          icon: ImageIcon(AssetImage("assets/icons/chat.png")),
          activeIcon: ImageIcon(AssetImage("assets/icons/chat_active.png")),
          label: "联系商家",
        ),
      ],
    );
  }
}
